﻿namespace PlayListBundler
{
    using System.IO;
    using System.Collections.Generic;

    /// <summary>Provides helper functions for FileName routines.</summary>
    public static class Pattern
    {
        /// <summary>
        /// Gets the files (as a string array) with the passed extension.
        /// </summary>
        /// <param name="fileList">The file list.</param>
        /// <param name="ext">The extension.</param>
        /// <returns></returns>
        public static string[] GetFilesWithExtension(string[] fileList, string ext)
        {
            List<string> newList = new List<string>();
            foreach (string s in fileList)
            {
                string f = s.Substring(s.LastIndexOf('.') + 1).ToLower();
                if (f == ext)
                    newList.Add(s);                
            }
            string []array = new string[newList.Count];
            newList.CopyTo(array);
            newList.Clear();
            newList = null;

            return array;
        }

        /// <summary>
        /// Gets the filename combined with the passed track number.
        /// </summary>
        /// <param name="number">The track number.</param>
        /// <param name="file">The original filename (no path).</param>
        /// <returns></returns>
        public static string GetNameWithTrackNumber(int number, string file)
        {
            string s = number.ToString();
            if (number < 10)
                s = "0" + s;
            string numberPattern = ". ";
            return /*Path.GetDirectoryName(file) +  @"\" +*/ 
                s + numberPattern + file;
        }

        /// <summary>
        /// Gets the filename of the passed path (filename without path).
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The filename without path.</returns>
        public static string GetFileName(string path)
        {
            return path.Remove(0, path.LastIndexOf('\\') + 1);
            //return Path.GetDirectoryName(path);
        }

        //#region FileName routines
        //public static String GetFilePattern(String Filename)
        //{
        //    String fname = Path.GetFileNameWithoutExtension(Filename);
        //    for (int i = 0; i < fname.Length; i++)
        //    {
        //        byte val = (byte)fname[i];
        //        if ((val >= 48) && (val <= 57))
        //            return fname.Substring(0, i - 1) + "*";
        //    }

        //    return "*";
        //}

        //public static String[] GetFileList(String firstFile)
        //{
        //    String path = Path.GetDirectoryName(Path.GetFullPath(firstFile));
        //    String ext = Path.GetExtension(firstFile);
        //    String pattern = GetFilePattern(firstFile);

        //    return GetFileList(path, pattern + ext);
        //}

        //public static String[] GetFileList(String path, String pattern)
        //{
        //    DirectoryInfo di = new DirectoryInfo(path);
        //    FileInfo[] fis = di.GetFiles(pattern);
        //    String[] files = new String[fis.Length];

        //    for (int i = 0; i < fis.Length; i++)
        //    {
        //        files[i] = path + "\\" + fis[i].Name;
        //    }

        //    return files;
        //}
        //#endregion
    }
}
